<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\View;
use app\common\model\mysql\CarUserSalary;

class CarUserData extends BaseController
{
    public function addCarUserData(){
        if($this->request->isPost()){
            $carUserSalary = new CarUserSalary();
            $data = $this->request->param();
            $result = $carUserSalary->insertCarUserData($data);
            if($result){
                return show(config('status.success'),'OK', [], 200);
            }else{
                return show(config('status.error'),'添加失败,内部异常', [], 200);
            }
        }
    }

    public function editCarUserData(){
        $carUserSalary = new CarUserSalary();
        if($this->request->isPost()){

            return show(config('status.code.error'),'修改失败', [], 200);
        }

        $id = $this->request->param("id",0,'intval');
        if(empty($id)){
            return show(config('status.code.error'), '请求参数错误', [], 200);
        }
        $result = $carUserSalary->getCarUserDataById($id);

    }
    //获取司机工资列表
    public function getCarUserDataList(){
        $carUserSalary = new CarUserSalary();
        $timeInterval = $this->request->param('time_interval',date('Y-m-d').'~'.date('Y-m-d'),'string');
        $timeDate = explode("~", $timeInterval);
        $where = [];
        if(!empty($timeDate)){
            $where = [
                'enter_date' => [$timeDate[0],$timeDate[1]]
            ];
        }
        $list = $carUserSalary->getCarUserDataByPage($where);
        $data = [];
        foreach ($list as $v){
            $data[] = [
                'id' => $v->id,
                'total_price' => $v->total_price,
                'price' => $v->price,
                'enter_date' => $v->enter_date,
                'user_id' => $v->user_id,
                'name' => $v->carUser->name
            ];
        }
        return show(config('status.success'),'OK', $data, 200);
    }

    //删除
    public function deleteCarUserData(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'intval');
            if(empty($id)){
                return show(config('status.error'),'参数错误', [], 200);
            }
            $carUserSalary = new CarUserSalary();
            $result = $carUserSalary->deleteById($id);

            if($result){
                return show(config('status.success'),'OK', [], 200);
            }
            returnshow(config('status.error'),'删除失败', [], 200);
        }
    }
}